package com.niannian.photo;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.R;
import com.niannian.photo.BitmapCache;
import com.niannian.photo.BitmapCache.ImageCallback;
import com.niannian.photo.ImageItem;
import com.niannian.util.VAR;

/**
 * 这个是显示一个文件夹里面的所有图片时用的适配器
 * 
 */
public class AlbumGridViewAdapter extends BaseAdapter {
	final String TAG = getClass().getSimpleName();
	private Context mContext;
	private ArrayList<ImageItem> dataList;
	private ArrayList<ImageItem> selectedDataList;
	private DisplayMetrics dm;
	BitmapCache cache;
	int curP = 0;

	public AlbumGridViewAdapter(Context c, ArrayList<ImageItem> dataList, ArrayList<ImageItem> selectedDataList) {
		mContext = c;
		cache = new BitmapCache();
		this.dataList = dataList;
		this.selectedDataList = selectedDataList;
		dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
	}

	public void setData(ArrayList<ImageItem> dataList, int curP) {
		this.dataList = dataList;
		this.curP = curP;
	}

	public int getCount() {
		return dataList.size();
	}

	public Object getItem(int position) {
		return dataList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public ArrayList<ImageItem> getdata() {
		return dataList;
	}

	ImageCallback callback = new ImageCallback() {
		@Override
		public void imageLoad(ImageView imageView, Bitmap bitmap, Object... params) {
			if (imageView != null && bitmap != null) {
				String url = (String) params[0];
				if (url != null && url.equals((String) imageView.getTag())) {
					((ImageView) imageView).setImageBitmap(bitmap);
				} else {
					Log.e(TAG, "callback, bmp not match");
				}
			} else {
				Log.e(TAG, "callback, bmp null");
			}
		}
	};

	/**
	 * 存放列表项控件句柄
	 */
	private class ViewHolder {
		public ImageView imageView;
		public ToggleButton toggleButton;
		public Button choosetoggle;
		public TextView textView;
		public LinearLayout ll_camera;
		public ImageView image_camera;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.p_plugin_camera_select_imageview, parent, false);
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
			viewHolder.toggleButton = (ToggleButton) convertView.findViewById(R.id.toggle_button);
			viewHolder.choosetoggle = (Button) convertView.findViewById(R.id.choosedbt);
			// RelativeLayout.LayoutParams lp = new
			// RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,dipToPx(65));
			// lp.setMargins(50, 0, 50,0);
			// viewHolder.imageView.setLayoutParams(lp);
			int pich = (int) ((BaseActivity.mScreenWidth / 3) - 20);
			LayoutParams params = convertView.getLayoutParams();
			params.height = pich;
			params.width = pich;
			convertView.setLayoutParams(params);

			viewHolder.ll_camera = (LinearLayout) convertView.findViewById(R.id.ll_camera);
			viewHolder.image_camera = (ImageView) convertView.findViewById(R.id.image_camera);
			LayoutParams image_camera_params = viewHolder.image_camera.getLayoutParams();
			image_camera_params.height = pich - 80;
			image_camera_params.width = pich - 50;
			viewHolder.image_camera.setLayoutParams(image_camera_params);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (curP == 0 && position == 0) {
			viewHolder.imageView.setImageResource(R.drawable.ajm);
			viewHolder.ll_camera.setVisibility(View.VISIBLE);
			viewHolder.imageView.setVisibility(View.GONE);

		} else {
			viewHolder.ll_camera.setVisibility(View.GONE);
			viewHolder.imageView.setVisibility(View.VISIBLE);
			String path;
			if (dataList != null && dataList.size() > position)
				path = dataList.get(position).imagePath;
			else
				path = "camera_default";
			if (path.contains("camera_default")) {
				viewHolder.imageView.setImageResource(R.drawable.plugin_camera_no_pictures);
			} else {
				final ImageItem item = dataList.get(position);
				viewHolder.imageView.setTag(item.imagePath);
				cache.displayBmp(viewHolder.imageView, item.thumbnailPath, item.imagePath, callback);
			}
		}

		viewHolder.toggleButton.setTag(position);
		viewHolder.choosetoggle.setTag(position);

		if (curP == 0 && position == 0) {
			viewHolder.toggleButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					((ToggleButton) v).setChecked(false);
					//Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					
					String pic = MyApplication.FILE_PATH_TEMP + "nn" + ((new Date()).getTime() / 1000) + "pictemp.jpg";
					AlbumActivity.photoPath=pic;
					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(pic)));
					
					((Activity) mContext).startActivityForResult(intent, VAR.TAKE_PICTURE);
				}
			});
		} else {
			viewHolder.toggleButton.setOnClickListener(new ToggleClickListener(viewHolder.choosetoggle));
		}

		if (selectedDataList.contains(dataList.get(position))) {
			viewHolder.toggleButton.setChecked(true);
			viewHolder.choosetoggle.setVisibility(View.VISIBLE);
		} else {
			viewHolder.toggleButton.setChecked(false);
			viewHolder.choosetoggle.setVisibility(View.GONE);
		}

		return convertView;
	}

	public int dipToPx(int dip) {
		return (int) (dip * dm.density + 0.5f);
	}

	private class ToggleClickListener implements OnClickListener {
		Button chooseBt;

		public ToggleClickListener(Button choosebt) {
			this.chooseBt = choosebt;
		}

		@Override
		public void onClick(View view) {
			if (view instanceof ToggleButton) {
				ToggleButton toggleButton = (ToggleButton) view;
				int position = (Integer) toggleButton.getTag();
				if (dataList != null && mOnItemClickListener != null && position < dataList.size()) {
					mOnItemClickListener.onItemClick(toggleButton, position, toggleButton.isChecked(), chooseBt);
				}
			}
		}
	}

	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener l) {
		mOnItemClickListener = l;
	}

	public interface OnItemClickListener {
		public void onItemClick(ToggleButton view, int position, boolean isChecked, Button chooseBt);
	}

}
